import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    // 登录注册
    {
      path: '/',
      name: 'login',
      component: () => import('@/views/Student/Login/index.vue')
    },
    {
      path: '/reg',
      component: () => import('@/views/Student/RegisterView/index.vue')
    },
    {
      path: '/home',
      name: 'Home',
      component: () => import('@/views/Student/Home/Home.vue'),
      redirect: '/home/course',
      meta: {
        requireAuth: true
      },
      children: [
        {
          path: '/home/course',
          name: 'Course',
          component: () => import('@/views/Student/Home/CourseView.vue'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/home/write',
          name: 'write',
          component: () => import('@/views/Student/Home/WriteView.vue'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/home/group',
          name: 'group',
          component: () => import('@/views/Student/Home/GroupView.vue'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/home/CD',
          name: 'CD',
          component: () => import('@/views/Student/Home/CDView.vue'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/home/friend',
          name: 'friend',
          component: () => import('@/views/Student/Home/FriendView.vue'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/home/avatar',
          component: () => import('@/views/Public/avatarView.vue'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/home/password',
          component: () => import('@/views/Public/passwordView.vue'),
          meta: {
            requireAuth: true
          }
        }
      ]
    }, //一级路由
    {
      path: '/detail/:courseId',
      name: 'Detail',
      component: () => import('@/views/Student/CourseDetail/index.vue'),
      redirect: '/detail/introduce',
      meta: {
        requireAuth: true
      },
      children: [
        {
          path: '/detail/Exam',
          name: 'Exam',
          component: () => import('@/views/Student/CourseDetail/examDetail.vue'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/detail/task',
          name: 'task',
          component: () => import('@/views/Student/CourseDetail/taskView.vue'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/detail/introduce',
          name: 'introduce',
          component: () => import('@/views/Student/CourseDetail/introduceView.vue'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/detail/content',
          name: 'content',
          component: () => import('@/views/Student/CourseDetail/contentView.vue'),
          meta: {
            requireAuth: true
          }
        },
        {
          path: '/detail/knowledge',
          name: 'knowledge',
          component: () => import('@/views/Student/CourseDetail/knowLedge.vue'),
          meta: {
            requireAuth: true
          }
        }
      ]
    },
    //详情页面
    {
      path: '/Train/Detail',
      name: 'TrainDetail',
      component: () => import('@/views/Student/TrainDetail/knowLeagdeView.vue'),
      meta: {
        requireAuth: true
      }
    },

    // 教师主页
    {
      path: '/teacher',
      name: 'Teacher',
      component: () => import('@/views/Teacher/home/HomeView.vue'),
      redirect: '/teacher/application',
      meta: {
        requireAuth: true
      },
      children: [
        {
          path: '/teacher/application',
          name: 'application',
          component: () => import('@/views/Teacher/home/ApplicationView.vue'),
          meta: {
            name: '应用中心',
            requireAuth: true
          }
        },
        {
          path: '/teacher/course',
          name: 'course',
          component: () => import('@/views/Teacher/home/CourseView.vue'),
          meta: {
            name: '课程管理',
            requireAuth: true
          }
        },
        {
          path: '/teacher/classmager',
          name: 'classmager',
          component: () => import('@/views/Teacher/home/ClassMager.vue'),
          meta: {
            name: '班级管理',
            requireAuth: true
          }
        },
        {
          path: '/teacher/inbox',
          name: 'inbox',
          component: () => import('@/views/Teacher/home/InBox.vue'),
          meta: {
            name: '收件箱',
            requireAuth: true
          }
        },
        {
          path: '/teacher/note',
          name: 'note',
          component: () => import('@/views/Teacher/home/NoteView.vue'),
          meta: {
            name: '笔记',
            requireAuth: true
          }
        },
        {
          path: '/teacher/cloudplant',
          name: 'cloudplant',
          component: () => import('@/views/Teacher/home/CloudPlant.vue'),
          meta: {
            name: '云盘',
            requireAuth: true
          }
        },
        {
          path: '/teacher/create',
          name: 'create',
          component: () => import('@/views/Teacher/home/CreateView.vue'),
          meta: {
            name: '专题创作',
            requireAuth: true
          }
        },
        {
          path: '/teacher/resource',
          name: 'resource',
          component: () => import('@/views/Teacher/home/ResourceView.vue'),
          meta: {
            name: '资源库',
            requireAuth: true
          }
        }
      ]
    },
    {
      path: '/avater',
      name: 'avater',
      component: () => import('@/views/Public/avatarView.vue'),
      meta: {
        name: '头像设置'
      }
    },
    {
      path: '/password',
      name: 'password',
      component: () => import('@/views/Public/passwordView.vue'),
      meta: {
        name: '密码设置'
      }
    },
    {
      path: '/classStuInfo',
      name: 'classStuInfo',
      component: () => import('@/views/Teacher/classInfo/ClassStuInfo.vue'),
      meta: {
        name: '班级学生信息',
        requireAuth: true
      }
    },
    // 教师课程管理
    {
      path: '/coursedetail/:courseId',
      name: 'detail',
      component: () => import('@/views/Teacher/course/CourseDetail.vue'),
      redirect: '/coursedetail/chapter',
      meta: {
        name: '课程详情',
        requireAuth: true
      },
      children: [
        {
          path: '/coursedetail/chapter',
          name: 'chapter',
          component: () => import('@/views/Teacher/course/ChapterView.vue'),
          meta: {
            name: '章节管理',
            requireAuth: true
          }
        },
        {
          path: '/coursedetail/notice',
          name: 'notice',
          component: () => import('@/views/Teacher/course/NoticeView.vue'),
          meta: {
            name: '通知',
            requireAuth: true
          }
        },
        {
          path: '/coursedetail/exam',
          name: 'exam',
          component: () => import('@/views/Teacher/course/ExamView.vue'),
          meta: {
            name: '考试',
            requireAuth: true
          }
        },
        {
          path: '/coursedetail/HomeworkView',
          name: 'HomeworkView',
          component: () => import('@/views/Teacher/course/HomeworkView.vue'),
          meta: {
            name: '作业',
            requireAuth: true
          }
        },
        {
          path: '/coursedetail/CourseWare',
          name: 'CourseWare',
          component: () => import('@/views/Teacher/course/CourseWare.vue'),
          meta: {
            name: '课件',
            requireAuth: true
          }
        },
        {
          path: '/coursedetail/DiscussionView',
          name: 'DiscussionView',
          component: () => import('@/views/Teacher/course/DiscussionView.vue'),
          meta: {
            name: '讨论区',
            requireAuth: true
          }
        },
        {
          path: '/coursedetail/QuestionBank',
          name: 'QuestionBank',
          component: () => import('@/views/Teacher/course/QuestionBank.vue'),
          meta: {
            name: '题库',
            requireAuth: true
          }
        },
        {
          path: '/coursedetail/StatisticView',
          name: 'StatisticView',
          component: () => import('@/views/Teacher/course/StatisticView.vue'),
          meta: {
            name: '统计',
            requireAuth: true
          }
        }
      ]
    },
    {
      path: '/spendNotice',
      name: 'spendnotice',
      component: () => import('@/views/Teacher/course/SpendNotice.vue'),
      meta: {
        name: '发送通知'
      }
    }
  ]
})

//添加路由守卫
router.beforeEach((to, from, next) => {
  // 判断该路由是否需要登录权限
  // if (!sessionStorage.token && to.path !== '/') return '/'
  if (to.meta.requireAuth && !sessionStorage.token) {
    next({
      name: 'login'
    })
  }
  next()
})

export default router
